package ui.controls;

import java.awt.Color;

import ui.controls.Dial;
import ui.control.dial.*;
import ui.Assets;
import ui.Console;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cpu.components.DeviceUnit;

public class DialBank extends JPanel implements cpu.interfaces.Device {
	protected Dial[] bank;
	
	protected int width = 0;
	protected int height = 0;
	protected String name;
	private Console owner;
	
	private cpu.components.DeviceUnit controller;
	
	boolean changed = false;
	
	public DialBank( Assets assets, Console owner, String name, int dials, DialSizes size, String[] labels ) {
		setLayout( null );

		this.owner = owner;
		this.name = name;
		bank = new Dial[1];
		bank[0] = new Dial( assets, this, size );
		JLabel label;

		int dialCenterX, dialCenterY;
		int sublabelWidth = 32;
		int sublabelHeight = 12;
		int dialWidth = bank[0].getWidth( ); 
		int dialHeight = bank[0].getHeight( );
		
		int titleHeight = 12; 
		//int labelCenterOffset = (int) ( dialWidth / 2 * Math.sqrt( 2 ) ) - 20;
		int labelCenterOffset = dialWidth / 2;
		
		dialCenterX = labelCenterOffset + sublabelWidth;
		dialCenterY = labelCenterOffset + sublabelHeight + titleHeight + 2;
		
		int bankWidth = 2 * dialCenterX; 
		int bankHeight = ( labelCenterOffset + sublabelHeight ) * 2 + titleHeight + 2;
		
		
		int titleWidth = bankWidth;
		
		label = new JLabel( name );
		label.setLocation( 0, 0 );
		label.setSize( titleWidth, titleHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );

		label = new JLabel( labels[0] );
		label.setLocation( dialCenterX - sublabelWidth / 2, dialCenterY - labelCenterOffset - sublabelHeight  );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );

		label = new JLabel( labels[1] );
		label.setLocation( dialCenterX + labelCenterOffset - sublabelWidth / 2, dialCenterY - labelCenterOffset - sublabelHeight );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );
		
		label = new JLabel( labels[2] );
		label.setLocation( dialCenterX + labelCenterOffset, dialCenterY - sublabelHeight / 2 );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.LEFT);
		this.add( label );

		label = new JLabel( labels[3] );
		label.setLocation( dialCenterX + labelCenterOffset - sublabelWidth / 2, dialCenterY + labelCenterOffset );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );
	
		label = new JLabel( labels[4] );
		label.setLocation( dialCenterX - sublabelWidth / 2, dialCenterY + labelCenterOffset );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );

		label = new JLabel( labels[5] );
		label.setLocation( dialCenterX - labelCenterOffset - sublabelWidth / 2, dialCenterY + labelCenterOffset );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );
		
		label = new JLabel( labels[6] );
		label.setLocation( dialCenterX - labelCenterOffset - sublabelWidth, dialCenterY - sublabelHeight / 2 );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		this.add( label );
		
		label = new JLabel( labels[7] );
		label.setLocation( dialCenterX - labelCenterOffset - sublabelWidth / 2, dialCenterY - labelCenterOffset - sublabelHeight );
		label.setSize( sublabelWidth, sublabelHeight );
		label.setHorizontalAlignment(SwingConstants.CENTER);
		this.add( label );
		
		int dialLeft = dialCenterX - dialWidth / 2;
		int dialTop = dialCenterY - dialHeight / 2;
		
		bank[0].setLocation( dialLeft, dialTop );
		this.add( bank[0] );
		this.width = bankWidth;
		this.height = bankHeight;
		setSize( this.width, this.height );
		
		
		
		
		/*
		JLabel label;
		this.width = 0;
		for( int i = 0; i < bank.length; i++ ) {
			bank[i] = new Dial( assets, this, size );
			width = bank[i].getWidth( ) + 32;
			
			for( int j = 0; j < bank[i].positions( ); j++ ) {
				switch( j ) {
				case 0:
					label = new JLabel( labels[j] );
					label.setSize( 32, 16 );
					label.setLocation( width/2 + 16, 0 );
					label.setHorizontalAlignment(SwingConstants.CENTER);
					this.add( label );
					break;
				}
				
			}
			bank[i].setLocation( start + width * i, 0 );
			this.add( bank[i] );
			
			this.width += width;
		}
		
		this.height = bank[0].getHeight( ) + 36;
		//setSize( bank[0].getWidth( ) * bank.length, height );
		setSize( this.width, this.height );
		*/
	}
	public int getWidth( ) {
		return width;
	}
	public int getHeight( ) {
		return height;
	}
	public DialStates rotateClockwise( int i ) {
		return bank[i].rotateClockwise( );
	}
	public DialStates rotateCounterclockwise( int i ) {
		return bank[i].rotateCounterclockwise( );
	}
	public void changed( Dial dial, ui.control.dial.DialStates state ) {
		owner.changed( this, 0, state);
		changed = true;
	}
	
	public void enroll( int id, DeviceUnit controller ) {
		this.controller = controller;
		controller.enroll( id, this );
	}
	
	public void get( ) {
		switch( bank[0].getState( ) ) {
		case UP:
			controller.DBR.fromJavaInt( 0 );
			break;
		case UPRIGHT:
			controller.DBR.fromJavaInt( 1 );
			break;
		case RIGHT:
			controller.DBR.fromJavaInt( 2 );
			break;
		case DOWNRIGHT:
			controller.DBR.fromJavaInt( 3 );
			break;
		case DOWN:
			controller.DBR.fromJavaInt( 4 );
			break;
		case DOWNLEFT:
			controller.DBR.fromJavaInt( 5 );
			break;
		case LEFT:
			controller.DBR.fromJavaInt( 6 );
			break;
		case UPLEFT:
			controller.DBR.fromJavaInt( 7 );
			break;
		}
		changed = false;
	}
	
	// dialbanks do not support output
	public void put( ) {
		
		
	}
	
	public void status( ) {
		if( changed ) {
			controller.DSR.fromJavaInt( 1 );
		} else {
			controller.DSR.fromJavaInt( 0 );
		}
	}
}
